package com.douqu.game.battle.route;

import com.douqu.game.battle.controller.BattleController;
import com.douqu.game.battle.entity.aobject.BattleAObject;
import com.douqu.game.battle.entity.aobject.BattleSoldierAObject;
import com.douqu.game.battle.server.GameServer;
import com.douqu.game.battle.util.BattleUtils;
import com.douqu.game.battle.util.MsgUtils;
import com.douqu.game.core.astar.Node;
import com.douqu.game.core.config.common.Position;
import com.douqu.game.core.e.E_BattleAObjectStatus;
import com.douqu.game.core.util.LogUtils;

import java.util.List;

public class RouteObject
{
    public String battleId;

    public int sourceId;

    public int targetTeamNo;

    public int targetId;

    public RouteObject() {
    }

    public RouteObject(String battleId, int sourceId, int targetTeamNo, int targetId)
    {
        this.battleId = battleId;
        this.sourceId = sourceId;
        this.targetTeamNo = targetTeamNo;
        this.targetId = targetId;
    }



    public void findRoute()
    {
//        LogUtils.info("thread name -> " + Thread.currentThread().getId() + "  " + Thread.currentThread().getName());
        LogUtils.info("开始寻路 -> battleId:" + battleId + " sourceId:" + sourceId + " targetTeamNo:" +targetTeamNo + " targetId:" + targetId);
        BattleController battleController = GameServer.getInstance().getWorldManager().getBattleController(battleId);
        if(battleController == null || battleController.isEnd())
        {
            LogUtils.warn("战斗不存在或战斗已经结束");
            return;
        }

        if(!battleController.isStarted())
        {
            LogUtils.warn("战斗还没开始");
            return;
        }

        BattleAObject source = battleController.getSoldier(sourceId);
        if(source == null || !source.isMove || source.isDie())
        {
            LogUtils.warn("士兵不存在或者已经死亡->" + sourceId + ",->" + source);
            return;
        }

        BattleAObject target = battleController.getBattleAObject(targetTeamNo, targetId);
        if(target == null || target.isDie())
        {
            LogUtils.warn("目标不存在或者已经死亡");
            return;
        }

        BattleSoldierAObject soldier = (BattleSoldierAObject) source;
        if(soldier.moveSpeed.getAtt() <= 0)
        {
            LogUtils.info("速度为0");
            return;
        }

        if(battleController.isEnd())
        {
            LogUtils.warn("战斗已经结束");
            return;
        }

        Position targetPosition = target.position;
        if(source.position.x == 0 || source.position.y == 0 || targetPosition.x == 0 || targetPosition.y == 0)
        {
            LogUtils.warn("坐标错误,不能寻路 -> " + source.position + "  " + targetPosition);
            return;
        }

        if(soldier.isSameGoPosition(targetPosition))
        {
            LogUtils.warn("跟上一次寻路目标点相同");
            return;
        }

        try{
            LogUtils.debug(source.getUniqueId() + "  " + source.getName() + " 向 " + target.getName() + " 寻路 -> sourcePosition:" + source.position + "  target:" + target + "  targetPosition:" + targetPosition);

            soldier.setRouteTarget(target, targetPosition);

//            LogUtils.warn(battleController.getAstar().getAstarMap());

            battleController.getAstar().getPath(new Node(source.position.x, source.position.y), new Node(targetPosition.x, targetPosition.y), pathList -> {

                if(battleController.isEnd())
                {
                    LogUtils.warn("寻路结果时,战斗已经结束----------------->");
                    return;
                }

                LogUtils.info("寻路收到回复 -> ");

                if (soldier.isDie())
                    return;

                final long speed = soldier.moveSpeed.getAtt();
                if(speed <= 0)
                {
                    LogUtils.warn("寻路完成时,速度为0了，不处理");
                    return;
                }

                LogUtils.debug(soldier.getUniqueId() + " name:" + soldier.getName() + " 获取路线:" + pathList);
                List<Position> changeRouteList = BattleUtils.changeRoute(pathList, speed);

                if(soldier.isDie())
                    return;

                soldier.initRoute(changeRouteList);

                soldier.setStatus(E_BattleAObjectStatus.GOING);

                LogUtils.debug("寻路列表 -> " + pathList);

                MsgUtils.sendSoldierRoute(battleController, sourceId, (int)soldier.moveSpeed.getAtt(), pathList);

                LogUtils.debug(soldier.getUniqueId() + " name:" + soldier.getName() + " 从客户获取转换后的路线:" + changeRouteList);
            });
        }catch (Exception e){

            LogUtils.error("寻路ERROR------------------->" + e);
            LogUtils.error("start -> " + source.position + ", target -> " + targetPosition);
            LogUtils.error("map -> " + battleController.getAstar().getAstarMap());
            LogUtils.errorException(e);
        }
    }


    @Override
    public String toString() {
        return "{" +
                "battleId='" + battleId + '\'' +
                ", sourceId=" + sourceId +
                ", targetTeamNo=" + targetTeamNo +
                ", targetId=" + targetId +
                '}';
    }
}